package com.ym.livedemo.live1

import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.ym.livedemo.Constant
import com.ym.livedemo.R
import com.ym.livedemo.SystemBarHelper
import com.ym.livedemo.api.BusinessViewModel
import com.ym.livedemo.databinding.SettingsActivityBinding
import splitties.toast.UnreliableToastApi

class AnchorSettingsActivity : AppCompatActivity() {
    private val viewModel by viewModels<BusinessViewModel>()

    @OptIn(UnreliableToastApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val viewBinding = SettingsActivityBinding.inflate(layoutInflater)
        setContentView(viewBinding.root)
        SystemBarHelper.immersiveStatusBar(this, 0f)
        if (savedInstanceState == null) {
            supportFragmentManager
                .beginTransaction()
                .replace(R.id.settings, AnchorSettingsFragment())
                .commit()
        }
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        viewBinding.saveBack.setOnClickListener {
            onBackPressed()
        }
    }


    class AnchorSettingsFragment : PreferenceFragmentCompat(),
        Preference.OnPreferenceChangeListener {
        private lateinit var videoBitrate: ListPreference
        private lateinit var videoFps: ListPreference
        private lateinit var liveType: ListPreference

        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey)
            videoBitrate = findPreference("videoBitrate")!!
            videoFps = findPreference("videoFps")!!
            liveType = findPreference("liveType")!!

            videoBitrate.onPreferenceChangeListener = this
            videoFps.onPreferenceChangeListener = this
            liveType.onPreferenceChangeListener = this

            Constant.VIDEO_BITRATE = videoBitrate.value.toInt()
            Constant.VIDEO_FPS = videoFps.value.toInt()
            Constant.LIVE_TYPE = liveType.value.toInt()
        }

        override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean {
            preference?.let {
                Log.e("TAG", "onPreferenceChange: $newValue")
                when (it) {

                    videoBitrate -> { // 推流码率
                        Constant.VIDEO_BITRATE = newValue.toString().toInt() * 600
                    }
                    videoFps -> { // 推流帧率
                        Constant.VIDEO_FPS = newValue.toString().toInt()
                    }

                    liveType -> {
                        Constant.LIVE_TYPE = newValue.toString().toInt()
                    }

                    else -> {

                    }
                }

            }
            return true
        }
    }
}